#----------------------------------------------------------
#Feel free to customize what's below.
#DBG_PREFIX is the prefix to files that are built for
# debugging purposes
#libraries are the libraries passed to pkg-config.
#dbg_app_flags are flags specific to the debug version
#app_flags are flags specific to the release version.
#----------------------------------------------------------
#Oyea, if you want the version number in your code, act
# like it's #DEFINE'd to 'APPNAME_VERSION' ( where APPNAME
# is the name of your program -- and this directory)
# APPNAME_DEBUG - true if command is "make debug", false otherwise
#----------------------------------------------------------

DBG_PREFIX=dbg-
libraries:=
dbg_app_flags:=-g
app_flags:=

#Everything below is kinda crucial. Don't screw with it, plox.
CPPSOURCES:=$(wildcard src/*.cpp)
CSOURCES:=$(wildcard src/*.c)
OBJECTS:=$(patsubst src/%.cpp,obj/%,$(CPPSOURCES)) $(patsubst src/%.c,obj/%,$(CSOURCES))
DBOBJECTS:=$(patsubst src/%.cpp,obj/$(DBG_PREFIX)%,$(CPPSOURCES)) $(patsubst src/%.c,obj/%,$(CSOURCES))

APPNAME:=lib$(shell python -c "import os;print(os.getcwd().split(os.sep)[-1])").a
APPVERS:=$(shell python -c "f=open(\"version.txt\");ver=int(f.read())+1;print(ver);f.close()")
DBGAPPNAME:= $(DBG_PREFIX)$(APPNAME)

local_libraries:=$(shell python -c "import os;print(*[z for z in os.walk(os.path.join(os.getcwd(),'lib'))][0][1])")

lib_flags:=$(foreach lib,$(local_libraries), -I./lib/$(lib)/include -l$(lib))\
 $(foreach lib,$(libraries),$(shell pkg-config --cflags $(lib)))\
  -D $(APPNAME)_VERSION=$(APPVERS) -I./include

lib_links:=$(foreach lib,$(local_libraries), -L./lib/$(lib) -L./lib/$(lib)/lib -l$(lib))\
 $(foreach lib,$(libraries),$(shell pkg-config --libs $(lib)))\
 -static-libgcc -static-libstdc++

flags:= $(lib_flags) -D $(APPNAME)_DEBUG=false
links:= $(lib_links) -shared

dbg_flags:=$(lib_flags) -D $(APPNAME)_DEBUG=true
dbg_links:=$(lib_links)

CC=gcc
ar=ar
OUTDIR=./
increment_version:="f=open(\"version.txt\",\"w\");f.write(\"{}\".format($(APPVERS)+1));f.close()"
.PHONY:clean run rebuild debug increment_version libs

libs:
	$(foreach lib,$(local_libraries), @echo Updating $(lib)...;cd lib/$(lib);make all;cd $(DIROUT)..)

all: libs $(DIROUT)$(APPNAME)
	@true

debug: libs $(DIROUT)$(DBGAPPNAME)

clean:
	@rm $(OBJECTS) -f
	@rm $(DBOBJECTS) -f
	$(foreach lib,$(local_libraries), @echo Cleaning $(lib)...;cd lib/$(lib);make clean;cd ../..)

obj/%:src/%.c
	@echo Building $@ ...
	@$(CC) $< -c -o $@ $(flags)

obj/%:src/%.cpp
	@echo Building $@ ...
	@$(CXX) $< -c -o $@ $(flags)

obj/$(DBG_PREFIX)%:src/%.cpp
	@echo Building $@ ...
	@$(CXX) $< -c -o $@ $(dbg_flags)

obj/$(DBG_PREFIX)%:src/%.c
	@echo Building $@ ...
	@$(CC) $< -c -o $@ $(dbg_flags)

rebuild:clean libs $(DIROUT)$(APPNAME)

$(DIROUT)$(DBGAPPNAME):$(DBOBJECTS)
	@echo Archiving $@ from $^ ...
	@$(AR) -cvq $(DIROUT)$@ $^
	@python -c $(increment_version)

$(DIROUT)$(APPNAME): $(OBJECTS)
	@echo Archiving $@ from $^ ...
	@$(AR) -cvq $(DIROUT)$@ $(OBJECTS)
	@python -c $(increment_version)